package com.scuec.groupfive.Controller;

import com.scuec.groupfive.entity.Post;
import com.scuec.groupfive.entity.PostComment;
import com.scuec.groupfive.entity.UserInfo;
import com.scuec.groupfive.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/posts")
@CrossOrigin
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 获取帖子列表
     */
    @GetMapping
    public List<Post> getPostList() {
        return postService.getAllPostsWithStats();
    }

    /**
     * 分页获取帖子评论
     * @param postId 帖子ID
     * @param page 页码
     * @param size 每页大小
     * @return 评论列表
     */
    @GetMapping("/{postId}/comments")
    public List<PostComment> getPostComments(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return postService.getPostCommentsWithUserInfo(postId, page, size);
    }

    /**
     * 通过评论ID获取用户信息
     * @param commentId 评论ID
     * @return 用户信息
     */
    @GetMapping("/comments/{commentId}/user")
    public UserInfo getUserByCommentId(@PathVariable String commentId) {
        return postService.getUserByCommentId(commentId);
    }
}